//import {get,post} from '../commAction/index.js'

var conf = {
	data: {

	},
	nullFun() {

	},

	//修改input时
	bindKeyInput: function(e) {
		var type = e.currentTarget.dataset.type
		var param = {};

		param[type] = e.detail.value
		this.setData(param);
		this.validInput && this.validInput(type);
	},

	goBack(e) {
		var delta = 1
		try {
			if(e && e.currentTarget)
				delta = e.currentTarget.dataset.delta * 1;
			else if(e) delta=e * 1;
		} catch(e) {
			console.warn(e.message)
		}

		if(isNaN(delta)) delta = 1;
		if(delta < 1) delta = 1;
		wx.navigateBack({delta:delta})
	},
	goPage(e, query) {
		let url = e.currentTarget.dataset.url
		if(Util.isStrEmpty(url)) {
			Util.Msg('url is empty')
			return;
		}

		if(query != null) url += query;
		wx.navigateTo({
			url: url
		})
	},
	reLaunchPage(e) {
		let url = e.currentTarget.dataset.url
		if(Util.isStrEmpty(url)) {
			Util.Msg('url is empty')
			return;
		}

		wx.reLaunch({
			url: url
		})

	},

	redirectPage(e) {
		let url = e.currentTarget.dataset.url
		if(Util.isStrEmpty(url)) {
			Util.Msg('url is empty')
			return;
		}

		wx.redirectTo({
			url: url
		})
	},

	onShareAppMessage: function(e) {
		return Util.shareSet;
	}

}

class Util {

	static conf = function(obj) {
		return Util.merge(obj, conf);
	}
	static page = function(obj) {
		Page(Util.conf(obj));
	}

	static mergeOne = function(target, src) {
		if(typeof target != 'object') throw 'target is not a object';
		if(typeof src != 'object') throw 'src is not a object';
		for(let key in src) {
			if(target[key]) {
				if(typeof target[key] == 'object') {
					Util.mergeOne(target[key], src[key])
				} else {
					console.warn('key ' + key + ' in target will be overwrited')
					target[key] = src[key];
				}
			} else target[key] = src[key];
		}

		return target;

	}

	static merge = function(...args) {
		if(args.length < 2) {
			throw 'args must greater then two'
		}

		let first = args.shift()
		args.forEach(function(item) {
			Util.mergeOne(first, item)
		})
		return first;

	}
	
	static isTel(num){
		num+=''
		return /^1[0-9]{10,10}$/.test(num);
	}

	static replaceToNum = function(str) {//可以包含小数点
		var regexp = /[^\d]+/g;
		var newstr = str.replace(regexp, "");
		return newstr
	}
	
	static replaceToInt=function(str){
		var str=Util.replaceToNum(str)
		str=str.replace(/e|\./,'')
		return str;
	}

	//数组去重
	static arrUnique = function(ar) {
		var ret = [];
		for(var i = 0, j = ar.length; i < j; i++) {
			if(ret.indexOf(ar[i]) < 0) {
				ret.push(ar[i]);
			}
		}
		return ret;
	}
	//分享
	static shareSet = function() {
		return {
			title: '跑腿',
			desc: '',
			success: function(res) {
				wx.showToast({
					title: '转发成功',
					icon: 'success',
					duration: 2000
				})
			}
		}
	}

	static Msg(msg, cb, title, showCancel) {
		var cnt = msg
		if(typeof msg != 'string') {
			cnt = msg.message || JSON.stringify(msg)
		}
		wx.showModal({
			title: title || '消息',
			content: cnt,
			showCancel: showCancel || false,
			success: function() {
				cb && cb(true)
			},
			fail: function() {
				cb && cb(false)
			}
		})
	}

	static isStrEmpty(val) {
		return val == null || (val + '').trim() === '';
	}

	static arrEmptyCheck(...arr) {
		let empty = arr.some(item => {
			return Util.isStrEmpty(item);
		})
		return empty;
	}

	static Err(msg, cb, title) {
		console.error(msg)
		if(typeof msg =='object' && msg.error!=null){
			msg=msg.error
		}
		Util.Msg(msg, cb, title || '错误')
	}

	static setStorage(key, obj) {
		if(key == null || (key + '').trim() === '') {
			Util.Msg('key is empty')
			return;
		}
		wx.setStorageSync('run_app_' + key, obj)
	}

	static getStorage(key) {
		if(key == null || (key + '').trim() === '') {
			Util.Msg('key is empty')
			return;
		}
		return wx.getStorageSync('run_app_' + key);
	}

	static removeStorage(key) {
		if(key == null || (key + '').trim() === '') {
			Util.Msg('key is empty')
			return;
		}
		wx.removeStorageSync('run_app_' + key)
	}

	static getLoginInfo(commAction, cb) {
		wx.login({
			success(res) {
				if(!res.code) return;
				//				commAction

				console.log(res.code)
				//				return;
				commAction.post('get_open_id', {
					code: res.code
				}).then(resp => {
					var openid = resp.data.data.open_id
					var session_key = resp.data.data.session_key
					if(Util.isStrEmpty(openid)) {
						Util.Err('openid is empty')
						return;
					}

					if(Util.isStrEmpty(session_key)) {
						Util.Err('session_key is empty')
						return;
					}

					cb && cb({
						openid: openid,
						session_key: session_key
					})

				}).catch(err => {
					Util.Err(err)
				})

			},
			fail() {
				console.error('login failed')
				Util.Err('login failed')
			}
		})
	}

	static refreshSession(commAction, cb) {
		wx.checkSession({
			success() {
				cb && cb()
			},
			fail() {
				Util.getLoginInfo(commAction, function(data) {
					cb && cb(true, data)
				})
			}
		})
	}
	
	static showLoading(obj={}){
		wx.showLoading({
			title:obj.title||'加载',
			mask:obj.mask||true
		})
	}
	
	static hideLoading(){
		wx.hideLoading()
	}


}

export default Util;